/**
*  \file bfind.c
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "bfind.h"
#include "../assertor/assertor.h"
#include "../pal/pal_mem.h"

/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/

/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
enum compare bfind(const void *key, const void *base, pal_size_t num, pal_size_t size, compare_function_t cmp, void **position )
{
   ASSERTOR_PRE( NIL!= base,"Pointer to base can't be NIL");
   ASSERTOR_PRE( 0!= size,"size can't be zero");
   ASSERTOR_PRE( NIL != cmp,"Function pointer to compare function can't be NIL" );
   {
      U8 const *pivot=base;

      while( num-- )
      {
         switch( cmp(key, pivot) )
         {
            case compare_a_equals_b:
            {
               *position= (void *)pivot;
               return compare_a_equals_b;
            }
            case compare_a_less_b:
            {
               *position= (void *)pivot;
               return compare_a_less_b;
            }
            case compare_a_greater_b:
            {
               pivot+=size;
               break;
            }
            default:
            {
               ASSERTOR_ILLEGAL_DEFAULT("unexpected result of compare function");
               break;
            }

         }
      }

      *position= (void *)pivot;
      return compare_a_greater_b;

   }

}


#if 0
enum compare bfind(const void *key, const void *base, pal_size_t num, pal_size_t size, compare_function_t cmp, void **position )
{
   ASSERTOR_PRE( NIL!= base,"Pointer to base can't be NIL");
   ASSERTOR_PRE( 0!= size,"size can't be zero");
   ASSERTOR_PRE( NIL != cmp,"Function pointer to compare function can't be NIL" );
   {
      enum compare relation=compare_a_less_b;
      U8 const *pivot=base;

      while (num > 0)
      {
         pivot = base + (num >> 1) * size;

         switch( cmp(key, pivot) )
         {
            case compare_a_equals_b:
            {
               *position= (void *)pivot;
               return compare_a_equals_b;
            }
            case compare_a_less_b:
            {
               //base = pivot + size*(num>>1);
               //num--;
               break;
            }
            case compare_a_greater_b:
            {
               base= pivot;
               break;
            }
            default:
            {
               ASSERTOR_ILLEGAL_DEFAULT('unexpected result of compare function');
               break;
            }

         }
         num >>= 1;
      }


      *position= pivot;
      return relation;
   }

}
#endif

/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2018 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
